<template>
  <div class="userList">
    <!-- 用户信息 -->
    <el-card>
      <el-form
        ref="form"
        class="form"
        label-width="80px"
        :model="form"
        :rules="rules"
       >
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role_id" placeholder="请选择" prop="role_id">
            <el-option
              v-for="item in conversion.role_id"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="getData()">搜索</el-button>
        <el-button @click="clear">清除</el-button>
        <el-button type="primary" @click="add">+新增用户</el-button>
      </el-form>
    </el-card>
    <!-- table列表 -->
    <el-card>
      <el-table style="width: 100%" :data="tableData">
        <el-table-column label="序号" width="100px" type="index" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="电话" prop="phone" />
        <el-table-column label="邮箱" prop="email" />
        <el-table-column label="角色" prop="role" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="状态" prop="status">
          <template v-slot="{ row }">
            {{ row.status === 1 ? '禁用' : '启用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="changeQ(row.id)">{{
              row.status === 0 ? '禁用' : '启用'
            }}</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增 -->
    <add ref="add" :conversion="conversion" @getData="getData" />
  </div>
</template>

<script>
import { userList, userRemove, userStatus } from '@/api/userList'
import add from './components/add.vue'
import conversion from '@/api/constant/userList'
import cookieJs from 'js-cookie'
export default {
  components: {
    add
  },
  data() {
    return {
      conversion,
      form: {
        username: '',
        email: '',
        role_id: ''
      },
      rules: {},
      tableData: [],
      page: {
        // username: '', //	否	string	昵称
        // email: '', //	否	string	邮箱
        // role_id: '', //	否	string	角色数字 2管理员、3老师、 4学生
        page: 1, //	否	string	页码 默认为1
        limit: +cookieJs.get('limit') || 5 //	否	string	页尺寸 默认为10
      },
      total: 5,
      data: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await userList({
        ...this.form,
        page: this.page.page,
        limit: this.page.limit
      })
      console.log(res)
      this.data = JSON.parse(JSON.stringify(res.data.items))
      // 总数据条数
      this.total = res.data.pagination.total
      // 用户数据列表
      this.tableData = res.data.items
    },
    handleSizeChange(limit) {
      this.page.limit = limit
      //   console.log(size)
      cookieJs.set('limit', limit)
      this.getData()
      //   console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(page) {
      this.page.page = page
      //   console.log(page)
      this.getData()
      //   console.log(`当前页: ${val}`)
    },
    // 新增
    add() {
      this.$refs.add.isShow = true
      this.$refs.add.mode = '新增'
    },
    // 编辑
    edit(row) {
      this.$refs.add.isShow = true
      this.$refs.add.mode = '编辑'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 改变状态
    async changeQ(id) {
      await userStatus({ id })
      this.$message.success('修改状态成功')
      this.getData()
    },
    // 删除用户
    del(id) {
      this.$confirm('你确定要删除吗?', '提示')
        .then(async() => {
          await userRemove({ id })
          this.$message.success('删除成功')
          if (this.page.page > 1 && this.tableData.length <= 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {
          console.log('错误')
        })
    },
    // 清除
    clear() {
      this.form = {
        username: '',
        eamil: '',
        role_id: ''
      }
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.userList {
  padding: 15px;
  .el-card {
    border-radius: 10px;
    .form {
      display: flex;
      flex-direction: row;
      ::v-deep .el-input__inner {
        width: 200px;
      }
      .el-button {
        height: 40px;
        margin-left: 20px;
      }
    }
  }
  .block {
    text-align: right;
    padding-top: 20px;
  }
}
</style>
